package cn.mw.cmdb.entity.instanceToPo;

import lombok.Data;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Relationship;

import java.util.ArrayList;
import java.util.List;

@Node(labels = {"InstanceTopoCommonView"})
@Data
public class InstanceTopoCommonView {

    public static final String DEFAULT_INSTANCE_VIEW_ID = "-1";
    public static final String DEFAULT_INSTANCE_VIEW_NAME = "默认视图";

    @Id
    private String id;

    private String instanceId;

    private String modelId;

    private String instanceViewId;

    @Relationship(type = "instanceRelation", direction = Relationship.Direction.OUTGOING)
    private List<InstanceCommonRelation> outInstanceRelations;

    @Relationship(type = "instanceRelation", direction = Relationship.Direction.INCOMING)
    private List<InstanceCommonRelation> InInstanceRelations;

    public void addOutInstanceRelation(InstanceCommonRelation instanceCommonRelation) {
        if (outInstanceRelations == null) {
            outInstanceRelations = new ArrayList<>();
            outInstanceRelations.add(instanceCommonRelation);
        }
    }

    public void setId(String id) {
        this.id = id;
        String[] isAttrs = id.split("_");
        modelId = isAttrs[0];
        instanceId = isAttrs[1];
        instanceViewId = DEFAULT_INSTANCE_VIEW_ID;
    }
}
